package vip.xiaonuo.biz.modular.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.apilog.entity.DevApiLog;
import vip.xiaonuo.biz.modular.cabinetdoor.vo.BizCabinetDoorResp;
import vip.xiaonuo.biz.modular.event.entity.BizEvent;
import vip.xiaonuo.biz.modular.event.entity.BizEventConfig;
import vip.xiaonuo.biz.modular.event.mapper.BizEventConfigMapper;
import vip.xiaonuo.biz.modular.event.mapper.BizEventMapper;
import vip.xiaonuo.biz.modular.event.service.BizEventAuthService;
import vip.xiaonuo.biz.modular.event.service.BizEventReplaceService;
import vip.xiaonuo.biz.modular.event.util.EventActionUtil;

import javax.annotation.Resource;

/**
 * @author 墨塵
 * @since 2022/12/2
 */
@Service
@Transactional
public class BizEventAuthServiceImpl implements BizEventAuthService {
    @Resource
    private BizEventConfigMapper bizEventConfigMapper;

    @Resource
    private BizEventMapper bizEventMapper;

    @Resource
    private BizEventReplaceService bizEventReplaceService;

    @Override
    public void recod(DevApiLog apiLog) {
        //判断返回数据
        JSONObject respJson = JSON.parseObject(apiLog.getRespJson());
        if(!respJson.getBoolean("succ")){
            //异常处理
            Integer code = respJson.getInteger("code");
            if(code==7000){
                //无法识别
                this.addUnknowRecord(apiLog);
            }
            if(code==7001 || code ==7002){
                //门禁卡未登记
                this.addUnregisteredRecord(apiLog);
            }
        }else{
            //正常处理
            JSONObject resultJson = respJson.getJSONObject("result");
            if(!resultJson.getBoolean("canopendoor")){
                //门禁卡无权限
                this.addNoPermission(apiLog);
            }else{
                //门禁刷卡
                this.addAccessRecord(apiLog);
            }
        }
    }

    private void addAccessRecord(DevApiLog apiLog){
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("1");
        //事件描述
        String eventDesc = eventConfig.getEventDesc();
        //操作人
        String userName = bizEventReplaceService.getUserName(apiLog.getUserId());
        eventDesc = eventDesc.replace("{{user}}", userName);
        //柜门名称
        BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        if(cabinetDoorResp==null){
            return;
        }
        String doorName = bizEventReplaceService.getCbaDoorNameForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        eventDesc = eventDesc.replace("{{cab-door}}", doorName);
        //组合实体类
        BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
        event.setApiLogId(apiLog.getId());
        event.setEventConfigId(eventConfig.getId());
        event.setEventTime(apiLog.getEventTime());
        event.setEventDesc(eventDesc);
        event.setEventUserId(apiLog.getUserId());
        event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
        event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
        event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
        event.setCabinetDoorId(cabinetDoorResp.getId());
        bizEventReplaceService.clearBizEvent(event);
        bizEventMapper.insert(event);
    }

    private void addUnknowRecord(DevApiLog apiLog){
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("4");
        //事件描述
        String eventDesc = eventConfig.getEventDesc();
        //柜门名称
        BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        if(cabinetDoorResp==null){
            return;
        }
        String doorName = bizEventReplaceService.getCbaDoorNameForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        eventDesc = eventDesc.replace("{{cab-door}}", doorName);
        //组合实体类
        BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
        event.setApiLogId(apiLog.getId());
        event.setEventConfigId(eventConfig.getId());
        event.setEventTime(apiLog.getEventTime());
        event.setEventDesc(eventDesc);
        event.setEventUserId(apiLog.getUserId());
        event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
        event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
        event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
        event.setCabinetDoorId(cabinetDoorResp.getId());
        bizEventReplaceService.clearBizEvent(event);
        bizEventMapper.insert(event);
    }

    private void addUnregisteredRecord(DevApiLog apiLog){
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("3");
        //请求json
        JSONObject reqJson = JSON.parseObject(apiLog.getReqJson());
        //事件描述
        String eventDesc = eventConfig.getEventDesc();
        //柜门名称
        BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        if(cabinetDoorResp==null){
            return;
        }
        String doorName = bizEventReplaceService.getCbaDoorNameForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        eventDesc = eventDesc.replace("{{cab-door}}", doorName);

        eventDesc = eventDesc.replace("{{card}}", reqJson.getString("cardnum"));
        //组合实体类
        BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
        event.setApiLogId(apiLog.getId());
        event.setEventConfigId(eventConfig.getId());
        event.setEventTime(apiLog.getEventTime());
        event.setEventDesc(eventDesc);
        event.setEventUserId(apiLog.getUserId());
        event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
        event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
        event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
        event.setCabinetDoorId(cabinetDoorResp.getId());
        bizEventReplaceService.clearBizEvent(event);
        bizEventMapper.insert(event);
    }

    private void addNoPermission(DevApiLog apiLog){
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("2");
        //事件描述
        String eventDesc = eventConfig.getEventDesc();
        //操作人
        String userName = bizEventReplaceService.getUserName(apiLog.getUserId());
        eventDesc = eventDesc.replace("{{user}}", userName);
        //柜门名称
        BizCabinetDoorResp cabinetDoorResp = bizEventReplaceService.getCbaDoorRespForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        if(cabinetDoorResp==null){
            return;
        }
        String doorName = bizEventReplaceService.getCbaDoorNameForCode(apiLog.getCabinetCode(), apiLog.getCabinetDoorCode());
        eventDesc = eventDesc.replace("{{cab-door}}", doorName);
        //组合实体类
        BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
        event.setApiLogId(apiLog.getId());
        event.setEventConfigId(eventConfig.getId());
        event.setEventTime(apiLog.getEventTime());
        event.setEventDesc(eventDesc);
        event.setEventUserId(apiLog.getUserId());
        event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
        event.setCabinetGroupId(cabinetDoorResp.getCabinetGroupId());
        event.setCabinetId(cabinetDoorResp.getCabinetInfoId());
        event.setCabinetDoorId(cabinetDoorResp.getId());
        bizEventReplaceService.clearBizEvent(event);
        bizEventMapper.insert(event);
    }
}
